from django.shortcuts import render

from core.response import CustomResponse
from core.response import CustomPagination
from file.models import TextVector
from file.services import textvector_service
from rest_framework.decorators import action
from rest_framework import viewsets
from rest_framework.permissions import AllowAny



class TextVectorViewSet(viewsets.ViewSet):
    permission_classes = [AllowAny]

    @action(detail=False, methods=['GET'], url_path='text-vector',url_name='text-vector')
    def get_text_vector_by_file_id(self, request):
        file_id = request.query_params.get('file_id')
        if not file_id:
            return CustomResponse.error(message='缺少file_id参数')
        text_vector_list = textvector_service.get_by_file_id(file_id)
        return CustomResponse.success(text_vector_list)


    # 自定义分页查询
    @action(detail=False, methods=['GET'], url_path='get',url_name='text-vector2')
    def get_text_vector_by_vector_id(self, request):
        file_id = request.query_params.get('file_id')
        if not file_id:
            return CustomResponse.error(message='缺少file_id参数')
        data = textvector_service.get_by_file_id(file_id)
        paginator = CustomPagination()
        data = paginator.get_data(data,request)
        return CustomResponse.success(data)


    @action(detail=False, methods=['GET'], url_path='list',url_name='list')
    def get_text_vector_list(self, request):
        file_id = request.query_params.get('file_id')
        if not file_id:
            return CustomResponse.error(message="缺少file_id参数")

        vectors_data,paginator = textvector_service.get_vectors_by_file_id(file_id,request)
        return render(request, 'file/vector_list.html',{
            'vectors':vectors_data,
            'paginator':paginator,
            'page_obj':paginator.page,
            'file_id':file_id,
            'num_pages':paginator.page.paginator.num_pages,
        })





